#pragma once
#include "DXAMLCommon.h"
#include "IObject.h"
#include "CXcpList.h"
#include "IManagedRuntime.h"
#include "IManagedRuntimeHost.h"
#include "CScriptableObject.h"




class CRuntimeHost :
	public IManagedRuntime,
	public IManagedRuntimeHost
{
private: 
	static const XUINT16 * CLR_QUIRK_STRINGS[2];
public: 
	CRuntimeHost(struct ICoreServices * pCore, void * pExistingAppDomain);
	virtual ~CRuntimeHost();
	virtual XUINT32 AddRef();
	virtual XUINT32 Release();
	virtual HRESULT Initialize();
	void CloseManagedRuntime(XUINT32 fForceShutdown);
	virtual void CloseManagedRuntime();
	virtual XUINT32 EnterUserCode();
	virtual void LeaveUserCode();
	virtual XUINT16 * GetNamespace(struct IDependencyObject * pDO);
	virtual HRESULT GetScriptableObjectID(const XUINT16 * pName, XUINT32 * pID);
	virtual HRESULT GetScriptableObjectFromID(XUINT32 Id, void * * ppScriptObject);
	virtual HRESULT CallbackEventListener(struct IDependencyObject * pSender, struct IDependencyObject * pEventArgs);
	virtual XUINT32 IsCustomAppDomain();
	virtual HRESULT SetHtmlAccessEnabled(XUINT32 fEnabled);
	virtual HRESULT GetCoreServices(struct ICoreServices * * ppCoreServices);
	virtual HRESULT ReleaseHandle(const void * pHandle);
	virtual HRESULT CreateCallback(const void * pDelegateHandle, struct IDelegate * * ppCallback);
	virtual HRESULT CreateManagedObject(const void * pObjectHandle, XINT32 nMemberCount, struct IManagedObject * * ppManagedObject);
	virtual HRESULT InvokeDelegate(const void * pFunctionHandle, XINT32 nArgCount, struct ScriptParam * pParams, struct ScriptParam * pResult);
	virtual HRESULT IsCrossDomainSource(XUINT32 * pfIsCrossDomainSource);
	virtual HRESULT GetBrowserService(struct IBrowserService * * ppBrowserService, XUINT32 * pfSupportsBridge);
	virtual HRESULT RegisterScriptableObject(XUINT16 * pszName, void * pObject);
	virtual HRESULT UnregisterScriptableObject(XUINT16 * pszName);
	virtual HRESULT NotifyError(XUINT16 * pszMessage);
	virtual HRESULT GetScriptableMemberInfo(const void * pObjectHandle, XINT32 nMemberIndex, XINT32 * pnMemberID, XINT32 * pnMemberType, XUINT32 * pnMemberNameLength, XUINT16 * pszMemberName);
	virtual HRESULT InvokeScriptableMember(const void * pObjectHandle, XINT32 nMemberID, XINT32 nInvokeType, XINT32 nArgCount, struct ScriptParam * pArgs, struct ScriptParam * pResult, struct ScriptExceptionInfo * pExcepInfo);
private: 
	virtual HRESULT EnsureDOMBridgeDelegatesInitialized();
	struct ICoreServices * m_pCore;
	void * m_pCorePartTimeStrongPointerContext;
	struct ManagedRuntimeInfo * m_pRuntimeInfo;
	CXcpList<CScriptableObject> * m_pScriptableObjectList;
	XINT32 _nRefs;
	XINT32 _nStackSize;
	XUINT32 _fShutdownRequested;
	unsigned long _appDomainID;
	struct IBrowserService * _pBrowserService;
	int (__stdcall *_pfnDispose)();
	int (__stdcall *_pfnRequestShutdown)();
	int (__stdcall *_pfnInitialize)(IManagedRuntimeHost *);
	int (__stdcall *_pfnSetHtmlAccessEnabled)(unsigned int);
	int (__stdcall *_pfnReleaseHandle)(const void *);
	int (__stdcall *_pfnGetScriptableMemberInfo)(const void *, int, int *, int *, unsigned int *, unsigned __int16 *);
	int (__stdcall *_pfnInvokeScriptableMember)(const void *, int, int, int, ScriptParam *, ScriptParam *, ScriptExceptionInfo *);
	int (__stdcall *_pfnInvokeDelegate)(const void *, int, ScriptParam *, ScriptParam *);
	//function :  * _pfnDispose;
	//function :  * _pfnRequestShutdown;
	//function :  * _pfnInitialize;
	//function :  * _pfnSetHtmlAccessEnabled;
	//function :  * _pfnReleaseHandle;
	//function :  * _pfnGetScriptableMemberInfo;
	//function :  * _pfnInvokeScriptableMember;
	//function :  * _pfnInvokeDelegate;
	void * m_pAppDomainPunk;
	const wchar_t * * m_pQuirkStrings;
	XUINT32 m_nAppVersionsToQuirk;


};